package com.wrial.middle;
/*
 * @Author  Wrial
 * @Date Created in 16:51 2020/7/26
 * @Description  主要就是对boundary的处理
 */

public class Offer_67 {
    public int strToInt(String str) {
        int boundary = Integer.MAX_VALUE / 10; //代表越界值/10
        int flag = 1; //代表符号
        int startIndex = 0;
        int res = 0;
        if (str == null || str.length() == 0) return 0;
        char[] chars = str.trim().toCharArray();//丢弃开头末尾空格
        if (chars[0] == '-') {
            flag = -1;
            startIndex = 1;
        } else if(chars[0]=='+'){
            startIndex = 1;
        }

        for (int i = startIndex; i < chars.length; i++) {
            if (chars[i] > '9' || chars[i] < '0') break;
            if (res > boundary || res == boundary && chars[i] > 7) {
                return flag == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            res = res * 10 + chars[i] - '0';
        }
        return res * flag;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
    }
}
